package gui;

import java.awt.EventQueue;
import java.awt.ItemSelectable;

import javax.swing.JFrame;
import java.awt.CardLayout;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class MainApplication {

	private JFrame frame;
	private Properties properties = new Properties();
	private Properties configuration = new Properties();
	private String mmls;
	private String fls;
	private String img_cat;
	private String ils;
	private String ifind;
	private String blkcalc;
	private String blkcat;
	private String blkls;
	private String blkstat;
	private String fcat;
	private String ffind;
	private String fsstat;
	private String hfind;
	private String icat;
	private String img_stat;
	private String istat;
	private String jcat;
	private String jls;
	private String mactime;
	private String mmcat;
	private String mmstat;
	private String tsk_comparedir;
	private String tsk_gettimes;
	private String tsk_loaddb;
	private String tsk_recover;
	private String fileSeperator = System.getProperty("file.separator");
	private String configFileName = "sleuthkit-gui.config";
	private ArrayList<String> casename = new ArrayList<String>();
	private ArrayList<String> investigatior = new ArrayList<String>();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainApplication window = new MainApplication();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainApplication() {
		setup();
		initialize();
	}

	private void setup() {
		// TODO Auto-generated method stub
		try {
			configuration.loadFromXML(new FileInputStream(properties.getProperty(configFileName ,
					System.getProperty("user.home")+
					fileSeperator+".sleuthkit-gui"+fileSeperator+ configFileName)));
		} catch (InvalidPropertiesFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(1);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(2);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.exit(3);
		}

		mmls=configuration.getProperty("mmls");
		fls =configuration.getProperty("fls");
		blkcalc = configuration.getProperty("blkcalc");
		blkcat = configuration.getProperty("blkcat");
		blkls = configuration.getProperty("blkls");
		blkstat = configuration.getProperty("blkstat");
		fcat = configuration.getProperty("fcat");
		ffind = configuration.getProperty("ffind");
		fls = configuration.getProperty("fls");
		fsstat = configuration.getProperty("fsstat");
		hfind = configuration.getProperty("hfind");
		icat = configuration.getProperty("icat");
		ifind = configuration.getProperty("ifind");
		ils = configuration.getProperty("ils");
		img_cat = configuration.getProperty("img_cat");
		img_stat = configuration.getProperty("img_stat");
		istat = configuration.getProperty("istat");
		jcat = configuration.getProperty("jcat");
		jls = configuration.getProperty("jls");
		mactime = configuration.getProperty("mactime");
		mmcat = configuration.getProperty("mmcat");
		mmls = configuration.getProperty("mmls");
		mmstat = configuration.getProperty("mmstat");
		tsk_comparedir = configuration.getProperty("tsk_comparedir");
		tsk_gettimes = configuration.getProperty("tsk_gettimes");
		tsk_loaddb = configuration.getProperty("tsk_loaddb");
		tsk_recover = configuration.getProperty("tsk_recover");
		
		setupHelper(configuration.getProperty("caseName"), ";", casename);
		setupHelper(configuration.getProperty("investigator"), ";", investigatior);
	}

	private void setupHelper(String casenames, String delimiter, ArrayList<String> result) {
		// TODO Auto-generated method stub
		String[] cases = casenames.split(delimiter);
		for (String string : cases) {
			result.add(string);
		}
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 450, 300);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new GridLayout(0, 1, 0, 0));
		
		JPanel panel = new JPanel();
		frame.getContentPane().add(panel);
		panel.setLayout(null);
		
		JLabel lblCaseName = new JLabel("Case Name:");
		lblCaseName.setBounds(6, 12, 79, 16);
		panel.add(lblCaseName);
		
		JComboBox comboBox = new JComboBox();
		
		
		comboBox.addActionListener(new MyActionListener(comboBox));
		comboBox.setBounds(97, 8, 347, 27);
		comboBox.setEditable(true);
		populateComboBox(comboBox,casename);
		panel.add(comboBox);
		
		JLabel lblInvestigator = new JLabel("Investigator:");
		lblInvestigator.setBounds(6, 40, 79, 16);
		panel.add(lblInvestigator);
		
		JComboBox comboBox_1 = new JComboBox();
		comboBox_1.setEditable(true);
		comboBox_1.setBounds(97, 36, 347, 27);
		populateComboBox(comboBox_1, investigatior);
		panel.add(comboBox_1);
		
		JButton btnCreate = new JButton("Create...");
		btnCreate.setBounds(327, 243, 117, 29);
		panel.add(btnCreate);
	}

	private void populateComboBox(JComboBox comboBox, ArrayList<String> values) {
		// TODO Auto-generated method stub
		comboBox.addItem("");
		for (String value : values) {
			comboBox.addItem(value);
		}
		
	}
}
